package com.ayuku.retail.ui.supplier;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ayuku.commons.Util;
import com.ayuku.retail.business.Supplier.SupplierImpl;
import com.ayuku.retail.persistence.model.Supplier;

public class SupplierLookup extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private Table table1;
	private Label label1;
	private Button btnSearch;
	private TableColumn clmPosisi;
	private TableColumn clmNama;
	private TableColumn clmKode;
	private Text txtSearch;

	/**
	 * Auto-generated main method to display this org.eclipse.swt.widgets.Dialog
	 * inside a new Shell.
	 */
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			SupplierLookup inst = new SupplierLookup(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public SupplierLookup(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM
					| SWT.APPLICATION_MODAL);


			dialogShell.setLayout(new FormLayout());

			btnSearch = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
			FormData button1LData = new FormData();
			button1LData.left = new FormAttachment(0, 1000, 378);
			button1LData.top = new FormAttachment(0, 1000, 14);
			button1LData.width = 50;
			button1LData.height = 25;
			btnSearch.setLayoutData(button1LData);
			btnSearch.setText("Search");
			btnSearch.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					btnSearchWidgetSelected(evt);
				}
			});

			label1 = new Label(dialogShell, SWT.NONE);
			FormData label1LData = new FormData();
			label1LData.left = new FormAttachment(0, 1000, 12);
			label1LData.top = new FormAttachment(0, 1000, 16);
			label1LData.width = 52;
			label1LData.height = 19;
			label1.setLayoutData(label1LData);
			label1.setText("Search: ");
			label1.setAlignment(SWT.RIGHT);

			txtSearch = new Text(dialogShell, SWT.BORDER);
			FormData text1LData = new FormData();
			text1LData.left = new FormAttachment(0, 1000, 65);
			text1LData.top = new FormAttachment(0, 1000, 12);
			text1LData.width = 293;
			text1LData.height = 19;
			txtSearch.setLayoutData(text1LData);
			txtSearch.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent evt) {
					txtSearchKeyPressed(evt);
				}
			});

			table1 = new Table(dialogShell, SWT.VIRTUAL | SWT.SINGLE
					| SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL
					| SWT.H_SCROLL);
			FormData table1LData = new FormData();
			table1LData.left = new FormAttachment(0, 1000, 7);
			table1LData.top = new FormAttachment(0, 1000, 49);
			table1LData.width = 422;
			table1LData.height = 207;
			table1LData.right = new FormAttachment(1000, 1000, -9);
			table1LData.bottom = new FormAttachment(1000, 1000, -10);
			table1.setLayoutData(table1LData);
			table1.setHeaderVisible(true);
			table1.setLinesVisible(true);
			table1.setFont(SWTResourceManager.getFont("Tahoma", 10, 1, false,
					false));
			table1.addMouseListener(new MouseAdapter() {
				public void mouseDoubleClick(MouseEvent evt) {
					table1MouseDoubleClick(evt);
				}
			});

			clmKode = new TableColumn(table1, SWT.NONE);
			clmKode.setText("Kode");
			clmKode.setWidth(82);

			clmNama = new TableColumn(table1, SWT.NONE);
			clmNama.setText("Nama");
			clmNama.setWidth(231);

			clmPosisi = new TableColumn(table1, SWT.NONE);
			clmPosisi.setText("Alamat");
			clmPosisi.setWidth(103);

			loadData("%%");
			dialogShell.layout();
			dialogShell.setSize(450, 540);
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void loadData(String arg) throws Exception {
		List<Supplier> lst = SupplierImpl.getList("%" + arg + "%");

		table1.setItemCount(lst.size());
		for (int j = 0; j < lst.size(); j++) {
			final TableItem item = table1.getItem(j);
			Supplier pj = lst.get(j);
			item.setText(0, Util.nullToString(pj.getCode()));
			item.setText(1, Util.nullToString(pj.getName()));
			item.setText(2, Util.nullToString(pj.getAddress()));
		}
	}

	private void btnSearchWidgetSelected(SelectionEvent evt) {
		try {
			loadData(txtSearch.getText());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void txtSearchKeyPressed(KeyEvent evt) {
		if (evt.keyCode == 13) {
			try {
				loadData(txtSearch.getText());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void setForm(TableItem itm) {
	}

	private void table1MouseDoubleClick(MouseEvent evt) {
		TableItem[] selection = table1.getSelection();
		if ((selection != null) && (selection.length > 0)) {
			TableItem itm = (TableItem) selection[0];
			setForm(itm);
		} else {
			Util.showMsgBox("Silakan terlebih dulu pilih data yg akan diedit!",
					0, dialogShell);
		}
		dialogShell.close();
	}

}
